package com.mxprojects.gui
{
	import com.mxprojects.constructors.ButtonLarge;
	import com.mxprojects.constructors.InputConstructor;
	import com.mxprojects.io.BoardCookie;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.PrivateMessages;
	
	import flash.events.MouseEvent;
	
	public class PrivateSendMessage extends PrivateMessages
	{
		private static const MAX_SUBJECT_CHARS:int = 150;
		private static const MINIMUM_CHARS:int = 3;
		private static const MESSAGE_ROW_HEIGHT:int = 10;
		
		private static var usernameInput:InputConstructor;
		private static var subjectInput:InputConstructor;
		private static var messageInput:InputConstructor;
		private static var serverHandler:ServerHandler = new ServerHandler();
		private static var replyMessageID:int;
		
		public static function sendMessage(recipient:String = "", subject:String = "", message:String = "", replyID:int = 0):void {
			//show the nav
			buildTitleNav(Word.COMPOSE);
			
			//setup the content holder
			setupHolder();
			
			//set the message ID
			replyMessageID = replyID;
			
			//this will help us incase we need to add/remove elements
			var nextY:int = 0;
			var wideLabel:int = 0;
			
			usernameInput = new InputConstructor(Language.main(Word.USERNAME), recipient, 1, false, 1, "formInputLabel", "formInput", Config.MAX_USERNAME_LENGTH, Config.USERNAME_RESTRICTED_CHARS);
			nextY += usernameInput.height + Config.TEXT_PADDING/2;
			holder.addChild(usernameInput);
			if(usernameInput.inputLabel.width > wideLabel){
				wideLabel = usernameInput.inputLabel.width;
			}
			
			subjectInput = new InputConstructor(Language.main(Word.SUBJECT), subject, 2, false, 1, "formInputLabel", "formInput", MAX_SUBJECT_CHARS);
			subjectInput.y = nextY;
			nextY += subjectInput.height + Config.TEXT_PADDING/2;
			holder.addChild(subjectInput);
			if(subjectInput.inputLabel.width > wideLabel){
				wideLabel = subjectInput.inputLabel.width;
			}
			
			//setup the old message
			var quoteText:String = "";
			if(message != ""){
				//quoteText = "\n\n[quote=" + recipient + "]" + message + "[/quote]";
			}
			
			messageInput = new InputConstructor(Language.main(Word.MESSAGE), quoteText, 3, false, MESSAGE_ROW_HEIGHT);
			messageInput.y = nextY;
			nextY += messageInput.height + Config.TEXT_PADDING;
			holder.addChild(messageInput);
			if(messageInput.inputLabel.width > wideLabel){
				wideLabel = messageInput.inputLabel.width;
			}
			
			//if we have a recipient, then set the focus to the message input
			if(recipient != ""){
				if(subject != ""){
					messageInput.stage.focus = messageInput.inputBox;
				}else{
					subjectInput.stage.focus = subjectInput.inputBox;
				}
			}else{
				usernameInput.stage.focus = usernameInput.inputBox;
			}
			
			//figure out who the widest label is and then get the max size to make the inputs
			var newInputWidth:int = holder.width - wideLabel;
			usernameInput.changeWidth(newInputWidth);
			subjectInput.changeWidth(newInputWidth);
			messageInput.changeWidth(newInputWidth);
			
			//adjust the inputs
			usernameInput.x = wideLabel - usernameInput.inputLabel.width;
			subjectInput.x = wideLabel - subjectInput.inputLabel.width;
			messageInput.x = wideLabel - messageInput.inputLabel.width;
			
			//add a send and cancel button
			var sendButton:ButtonLarge = new ButtonLarge(Language.main(Word.SEND_PRIVATE_MESSAGE), checkBeforeSend);
			sendButton.x = Math.round(usernameInput.inputBox.x + usernameInput.inputBox.width/2 - sendButton.width/2);
			sendButton.y = Math.round(nextY);
			holder.addChild(sendButton);
			
			var cancelButton:ButtonLarge = new ButtonLarge(Language.main(Word.CANCEL), backToLocation);
			cancelButton.x = usernameInput.inputBox.x;
			cancelButton.y = Math.round(nextY);
			holder.addChild(cancelButton);
		}
		
		private static function checkBeforeSend(event:MouseEvent):void {
			//make sure we have something in user, subject and message
			var username:String = usernameInput.getValue();
			var subject:String = subjectInput.getValue();
			var message:String = messageInput.getValue();
			var sendButton:ButtonLarge = event.currentTarget as ButtonLarge;
			
			//disable the button
			sendButton.enabled = false;
			
			if(username.length > 0 
				&& subject.length >= MINIMUM_CHARS 
				&& message.length >= MINIMUM_CHARS){
				
				//check to make sure they are not trying to send it to themself
				if(username.toLocaleLowerCase() == BoardCookie.getUser().toLocaleLowerCase()){
					//toss an error
					DefaultDialog.show(Language.main(Word.ERROR_SEND_TO_SELF), false);
					
					//re-enable the button
					sendButton.enabled = true;
					
					return;
				}
				
				//start forming the data to send
				dataToSend = new Object();
				dataToSend.username = BoardCookie.getUser();
				dataToSend.password = BoardCookie.getPassword();
				dataToSend.location = Word.SEND_PRIVATE_MESSAGE;
				dataToSend.recipient = usernameInput.getValue();
				dataToSend.subject = subjectInput.getValue();
				dataToSend.message = messageInput.getValue();
				dataToSend.replyMessageID = replyMessageID;
				dataToSend.privateMessageLimit = Config.PRIVATE_MESSAGE_LIMIT;
				dataToSend.installDirectory = Config.INSTALL_DIRECTORY;
				dataToSend.boardName = Config.BOARD_NAME;
				dataToSend.boardEmail = Config.BOARD_EMAIL;
				dataToSend.newPrivateMessage = Language.main(Word.NEW_PRIVATE_MESSAGE);
				dataToSend.clickToRead = Language.main(Word.CLICK_TO_READ);
				dataToSend.subjectWord = Language.main(Word.SUBJECT);
				dataToSend.messageWord = Language.main(Word.MESSAGE);
				
				//send it off
				serverHandler.transmitData("privatemessages.php", dataToSend);
				serverHandler.listenForData(processSentData);
			}else{
				//throw an error
				DefaultDialog.show(Language.main(Word.ERROR_NEW_POST) + " " + MINIMUM_CHARS, false);
				
				//re-enable the button
				sendButton.enabled = true;
			} 
		}
		
		private static function processSentData(event:CustomEvent):void {
			dataFromServer = event.getServerData();
			
			switch(dataFromServer.output){
				case "mySqlError":
					//throw up an error dialog
					DefaultDialog.show(dataFromServer.error, false);
				break;
				
				case "mailboxFull":
					DefaultDialog.show(Language.main(Word.ERROR_MAILBOX_FULL), false);
				break;
				
				case "userNotFound":
					DefaultDialog.show(Language.main(Word.ERROR_USER_NOT_FOUND), false);
				break;
				
				case "success":
					//pop a little window to say everything was grand
					DefaultDialog.show(Language.main(Word.MESSAGE_PRIVATE_MESSAGE_SENT), false, Word.SUCCESS);
					
					//go back to the inbox
					changeLocation(Word.INBOX);
				break;
			}
		}
		
		private static function backToLocation(event:MouseEvent):void {
			//just sends them back to the location they just came from
			//check if they were replying to a message
			if(replyMessageID > 0){
				//go back to that message
				PrivateViewMessage.showLastMessage();
			}else{
				//go back to the inbox/outbox/trash
				if(dataFromServer != null){
					if(dataFromServer.currentPage == undefined){
						changeLocation(currentLocation);
					}else{
						changeLocation(currentLocation, dataFromServer.currentPage);
					}
				}else{
					//if we have no data let's just close the window
					PrivateMessages.closePopup();
				}
			}
		}

	}
}